const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const user = require('../../../../../services/user.js');
const common = require('../../../../../utils/common.js');
const app = getApp()
Page({
  data: {
    //补签是否开启
    openHelp: true,
    //剩余时间戳
    remain: 0,
    //定时器
    setTimeo: '',
  },
  onLoad: function (options){
    var _this = this
    var shareOpenid = options.shareOpenid
    var data = JSON.parse(options.data)
    _this.setData({
      shareOpenid: shareOpenid,
      data: data,
    })
    var time = _this.data.data.helpTime
    _this.getsTime(time);
    if (app.globalData.logininfo.openid != '' && app.globalData.logininfo.openid != null) {
      _this.setData({
        logininfo: app.globalData.logininfo,
        helpOpenid: app.globalData.logininfo.openid,
      });
    } 
    var shareOpenid = _this.data.shareOpenid
    if (shareOpenid != null && shareOpenid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = shareOpenid;
        data.access_token = res.access_token;
        //获取用户信息
        util.post(api.GetUserInfoByHomepage, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData: res.result.json,
            })
          }
        });
      });
    }
  },
  onShow: function(){
    var _this = this
    var time = _this.data.remain
    _this.timeDown(time);
  },
  //倒计时
  timeDown: function(time){
    var _this = this
    var remain = time - 1000
    _this.conutDown(remain);
    var setTimeo = setTimeout(function(){
      _this.onShow();
    },1000)
  },
  //计算剩余时
  conutDown: function (remain) {
    var _this = this
    var shi = parseInt(remain / 3600000)
    var fen = parseInt((remain % 3600000) / 60000)
    var miao = parseInt(((remain % 3600000) % 60000) / 1000)
    if (miao<10){
      miao = "0"+miao
    }
    var remainTime = shi + ":" + fen + ":" + miao
    _this.setData({
      remainTime: remainTime,
      remain: remain
    })
  },
  //剩余时间戳
  getsTime: function (time) {
    var _this = this
    var tomorrow = time % 31536000000
    var tomorrow2 = parseInt(tomorrow + 86400000)
    var d = new Date(); 
    var today = d.getTime() % 31536000000;
    if (today > tomorrow2){
      openHelp: false
    }else{
      var remain = tomorrow2 - today
      _this.setData({
        remain: remain
      })
    } 
  },
  //销毁定时器
  onHide: function () {
    var _this = this
    clearInterval(_this.data.setTimeo);
  },
  onUnload: function () {
    var _this = this
    clearInterval(_this.data.setTimeo);
  },
  //补签
  helpSign: function(){
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var shareOpenid = _this.data.shareOpenid
    var info = _this.data.data
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.access_token = res.access_token;
        data.openid = shareOpenid;
        data.type = 2;
        data.helpDate = info.helpDate;
        data.helpOpenid = openid;
        util.post(api.SupplementSign, data).then(function (res) {
          console.log(res)
          console.log(data)
          if (res.code == 200) {
            wx.switchTab({
              url: '/pages/home/home',
            });
          } else {
            wx.showToast({
              title: '补签失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      });
    }
  },
  //跳转首页
  gotoHome: function(){
    wx.switchTab({
      url: '/pages/home/home',
    });
  },
})